package com.android.component;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.widget.TextView;

import com.utils.Res;

public class Views extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.text);
        TextView tv1 = (TextView) findViewById(R.id.tv1);
        TextView tv2 = (TextView) findViewById(R.id.tv2);

        tv1.setText(getSpannableString());
        setHtmlText(tv2);
    }

    SpannableString getSpannableString() {
        SpannableString ss = new SpannableString("红色打电话斜体删除线绿色下划线图片:.");
        // 用颜色标记文本
        ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 2,
        // setSpan时需要指定的 flag,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括).
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        // 用超链接标记文本
        ss.setSpan(new URLSpan("http://www.baidu.com"), 2, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        // 用样式标记文本（斜体）
        ss.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 5, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        // 用删除线标记文本
        ss.setSpan(new StrikethroughSpan(), 7, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        // 用下划线标记文本
        ss.setSpan(new UnderlineSpan(), 10, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        // 用颜色标记
        ss.setSpan(new ForegroundColorSpan(Color.GREEN), 10, 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        // 获取Drawable资源
        Drawable d = getResources().getDrawable(R.drawable.icon);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        // 创建ImageSpan
        ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
        // 用ImageSpan替换文本
        ss.setSpan(span, 18, 19, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        // txtInfo.setText(ss);
        // txtInfo.setMovementMethod(LinkMovementMethod.getInstance()); // 实现文本的滚动

        return ss;
    }

    void setHtmlText(final TextView tV) {
        tV.setText(Html.fromHtml("用html方式设置文字颜色<font color=blue>这是蓝色的字</font>图片：<img src=\"dog2.png\"/>si mi da",
                new ImageGetter() {

                    @Override
                    public Drawable getDrawable(String source) {

                        // int id = getResources().getIdentifier(source, "drawable", tV.getContext().getPackageName());

                        // Drawable emoji = getResources().getDrawable(id);
                        // int w = (int) (emoji.getIntrinsicWidth() * 1.25);
                        // int h = (int) (emoji.getIntrinsicHeight() * 1.25);
                        // emoji.setBounds(0, 0, w, h);
                        Bitmap emoji = Res.loadAsset(Views.this, "emojis/" + source);

                        Drawable d = new BitmapDrawable(Views.this.getResources(), emoji);
                        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
                        return d;
                    }
                }, null));
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }
}
